package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.ActionDetailBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

/**
 * 活动详情
 */
public class ActionDetailActivity extends BaseActivity implements View.OnClickListener {
    private static final int REQUEST_LOGIN_CODE = 6666;
    public static final int REQUEST_BOOKING_CODE = 9683;
    private LinearLayout llAddView;//增加View
    private ImageView ivActionPic;//活动图片
    private TextView tvTitleName, tvPrice, tvCount;//标题,报名费,报名数
    private TextView tvIntroduction, tvTime, tvAddress;//简介，时间，地点
    private TextView btnBooking;//报名
    private int mFromClass;//1不显示右上角,2右上角不显示，底部按钮不显示
    private String mDetailId;//详情id
    private String mActionTitle,mActionPicUrl,mActionAddress,mActionMoney;//封面图，活动名，活动地点，活动金额

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_detail);
        setTitle("活动详情", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, TITLE_TYPE_TEXT, null);

        Intent intent = getIntent();
        mFromClass = intent.getIntExtra("from_class", 0);
        mDetailId = intent.getStringExtra("id");

        initView();
        initListener();

        getActionDetail();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        llAddView = (LinearLayout) findViewById(R.id.ll_add_view);
        ivActionPic = (ImageView) findViewById(R.id.iv_detail_pic);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ivActionPic.getLayoutParams();
        params.height = DisplayUtil.getDensityWdith(this)*9/16;

        tvTitleName = (TextView) findViewById(R.id.tv_title_name);
        tvPrice = (TextView) findViewById(R.id.tv_state);
        tvCount = (TextView) findViewById(R.id.tv_count);
        tvIntroduction = (TextView) findViewById(R.id.tv_introduction);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        btnBooking = (TextView) findViewById(R.id.tv_booking);
        btnBooking.setSelected(true);
        btnBooking.setClickable(false);
    }

    /**
     * 当活动详情未外部活动时
     * 设置WebView
     */
    private void setWebview(String url) {
        View viewLayout = LayoutInflater.from(this).inflate(R.layout.layout_webview, null);
        llAddView.removeAllViews();
        llAddView.addView(viewLayout);
        WebView wvDescription = (WebView) viewLayout.findViewById(R.id.webView);
        WebSettings settings = wvDescription.getSettings();
        //支持javascript
        settings.setJavaScriptEnabled(true);
        //自适应屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);

        wvDescription.loadUrl(url);
    }

    private void initListener() {
        btnBooking.setOnClickListener(this);
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        startActivity(PreviousActionActivity.class);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_booking:
                boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
                if (!isLogin) {
                    dialogConfirm(null, null, null, null).show();
                    return;
                }
                EditBookingActivity.startActivity(this,mDetailId,mActionPicUrl,mActionTitle,mActionAddress,mActionMoney);
//                Bundle bundle = new Bundle();
//                bundle.putString("action_id", mDetailId);
//                startActivityForResult(EditBookingActivity.class, bundle, REQUEST_BOOKING_CODE);
                break;
        }
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        startActivityForResult(LoginActivity.class, new Bundle(), REQUEST_LOGIN_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_BOOKING_CODE) {
                btnBooking.setSelected(true);
                btnBooking.setClickable(false);
                btnBooking.setText("已报名");
            } else if (REQUEST_LOGIN_CODE == requestCode) {
                getActionDetail();
            }
        }
    }

    /**
     * 活动详情
     */
    private void getActionDetail() {
        loadWaitDialog();
        HttpMethod.activityDetail(this, this, mDetailId);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_ACTIVITY_DETAILS) {//活动详情
            ActionDetailBean detailBean = GsonUtil.GsonToBean(data, ActionDetailBean.class);
            if (detailBean != null) {
                if (!StringUtils.isEmpty(detailBean.getLink())) {
                    setWebview(detailBean.getLink());
                } else {
                    //封面图，活动名，活动地点，活动金额
                    mActionTitle = detailBean.getName();
                    mActionPicUrl = detailBean.getActivityimg();
                    mActionAddress = detailBean.getSite();
                    mActionMoney = detailBean.getPrice();
                    ImageLoadingUtil.loadingUrlCenterCrop(ivActionPic,mActionPicUrl);
                    tvTitleName.setText(mActionTitle);

                    String score = detailBean.getFans() + "人已报名";
//                    SpannableString scoreSpan = TextViewUtil.getForegroundColorSpan(this, score, 0, detailBean.getFans().length(), getResources().getColor(R.color.colorC49F4A));
                    tvCount.setText(score);
                    tvPrice.setText(String.format(getString(R.string.book_course_price),mActionMoney));

                    tvIntroduction.setText(detailBean.getDescribe());
                    tvTime.setText(detailBean.getActivitytime());
                    tvAddress.setText(detailBean.getSite());
                }

                int state = detailBean.getStatus();
                switch (state) {// 状态1报名中 2 已报名 3 已结束
                    case 1:
                        btnBooking.setSelected(false);
                        btnBooking.setClickable(true);
                        btnBooking.setText("报名");
                        break;
                    case 2:
                        btnBooking.setSelected(true);
                        btnBooking.setClickable(false);
                        btnBooking.setText("已报名");
                        break;
                    case 3:
                        btnBooking.setSelected(true);
//                        btnBooking.setClickable(false);
                        btnBooking.setText("已结束");
                        break;
                }
            }
        }
    }
}
